package com.yebxxx.server.pojo;

import com.alibaba.fastjson.annotation.JSONCreator;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.yebxxx.config.CustomAuthorityDeserializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 * 
 * </p>
 *
 * @author stx
 * @since 2022-03-23
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@TableName("t_admin")
@ApiModel(value="Admin对象", description="")
public class Admin implements Serializable , UserDetails {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "姓名")
    private String name;

    @ApiModelProperty(value = "手机号码")
    private String phone;

    @ApiModelProperty(value = "住宅电话")
    private String telephone;

    @ApiModelProperty(value = "联系地址")
    private String address;

    @ApiModelProperty(value = "是否启用")
    @Getter(AccessLevel.NONE)
    private Boolean enabled;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "用户头像")
    private String userFace;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "角色")
    @TableField(exist = false)
    private List<Role> roles;
//UserDetails实现方法
//@JsonDeserialize(using = CustomAuthorityDeserializer.class)
    //返回角色列表

 @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = roles.stream().map(role -> new SimpleGrantedAuthority(role.getName()))
                .collect(Collectors.toList());
        return  authorities;
    }

////@JsonDeserialize(using = CustomAuthorityDeserializer.class)
//public Collection<? extends GrantedAuthority> getAuthorities() {
//    List<SimpleGrantedAuthority> authorities = new ArrayList<>();
//    for (Role role : roles) {
//        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getName());
//        authorities.add(authority);
//    }
//    return authorities;
//}

//    public class CustomAuthorityDeserializer extends JsonDeserializer {
//
//        public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
//            ObjectMapper mapper = (ObjectMapper) p.getCodec();
//            JsonNode jsonNode = mapper.readTree(p);
//            LinkedList<GrantedAuthority> grantedAuthorities = new LinkedList<>();
//            Iterator<JsonNode> elements = jsonNode.elements();
//            while (elements.hasNext()) {
//                JsonNode next = elements.next();
//                JsonNode authority = next.get("authority");
//                //将得到的值放入链表 最终返回该链表
//                grantedAuthorities.add(new SimpleGrantedAuthority(authority.asText()));
//            }
//            return grantedAuthorities;
//        }
//    }



    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
